// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.modules.common.controller;

import com.liuxinlong.annotation.LogAnnotation;
import com.liuxinlong.constant.LogBusinessConstants;
import com.liuxinlong.constant.LogOperateConstants;
import com.liuxinlong.modules.common.controller.request.BaseDeleteReq;
import com.liuxinlong.modules.common.controller.request.BaseReq;
import com.liuxinlong.modules.common.controller.request.QueryBaseReq;
import com.liuxinlong.modules.common.controller.request.QueryWorkflowConfigReq;
import com.liuxinlong.modules.common.controller.request.QueryWorkflowRecordReq;
import com.liuxinlong.modules.common.controller.request.UpdateWorkflowConfigReq;
import com.liuxinlong.modules.common.controller.request.UpdateWorkflowGroupReq;
import com.liuxinlong.modules.common.controller.request.UpdateWorkflowRecordReq;
import com.liuxinlong.modules.common.controller.response.BaseResp;
import com.liuxinlong.modules.common.service.WorkFlowGroupService;
import com.liuxinlong.modules.common.service.WorkFlowService;
import com.liuxinlong.modules.entity.WorkFlowConfig;
import com.liuxinlong.modules.entity.WorkFlowGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 工作流控制层
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2024-04-23
 */
@RestController
@RequestMapping("/workflow")
public class WorkFlowController {

    @Autowired
    private WorkFlowService workFlowService;

    @Autowired
    private WorkFlowGroupService workFlowGroupService;

    /**
     * 查询工作流配置信息
     *
     * @return 工作流配置信息
     */
    @PostMapping("config-page")
    public BaseResp pageWorkflowConfigList(QueryWorkflowConfigReq req) {
        Map<String, Object> queryParam = new HashMap<>();
        queryParam.put("name", req.getName());
        queryParam.put("startNum", req.getStartNum());
        queryParam.put("pageSize", req.getPageSize());
        List<Map<String, Object>> dataList = workFlowService.queryWorkflowConfigList(queryParam);
        int totalNum = workFlowService.getWorkflowConfigCount(queryParam);
        BaseResp resp = new BaseResp();
        resp.setDataList(dataList);
        resp.setTotalNum(totalNum);
        resp.setRetMessage("查询工作流配置信息成功！");
        return resp;
    }

    /**
     * 新增工作流配置
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("config-add")
    @LogAnnotation(value = "新增工作流配置", business = LogBusinessConstants.WORK_FLOW, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp addWorkflowConfig(@RequestBody UpdateWorkflowConfigReq req) {
        WorkFlowConfig workFlowConfig = new WorkFlowConfig();
        workFlowConfig.setFlowName(req.getFlowName());
        workFlowConfig.setContent(req.getContent());
        workFlowConfig.setBusinessType(req.getBusinessType());
        workFlowService.addWorkflowConfig(workFlowConfig);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("新增工作流配置成功");
        return resp;
    }

    /**
     * 修改工作流配置
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("config-update")
    @LogAnnotation(value = "修改工作流配置", business = LogBusinessConstants.WORK_FLOW, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updateWorkflowConfig(@RequestBody UpdateWorkflowConfigReq req) {
        WorkFlowConfig workFlowConfig = new WorkFlowConfig();
        workFlowConfig.setId(req.getId());
        workFlowConfig.setFlowName(req.getFlowName());
        workFlowConfig.setContent(req.getContent());
        workFlowConfig.setBusinessType(req.getBusinessType());
        workFlowService.updateWorkflowConfig(workFlowConfig);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("修改工作流配置成功");
        return resp;
    }

    /**
     * 删除工作流配置信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("config-delete")
    @LogAnnotation(value = "删除工作流配置信息", business = LogBusinessConstants.WORK_FLOW, operateType = LogOperateConstants.OPERATE_DELETE)
    public BaseResp deleteWorkflowConfig(@RequestBody BaseDeleteReq req) {
        workFlowService.deleteWorkflowConfig(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("删除工作流配置信息成功");
        return resp;
    }

    /**
     * 查询工作流节点配置
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("config-node-page")
    public BaseResp pageWorkflowNodeConfig(@RequestBody BaseDeleteReq req) {
        List<Map<String, Object>> dataList = workFlowService.pageWorkflowNodeConfig(req.getId());
        BaseResp resp = new BaseResp();
        resp.setDataList(dataList);
        resp.setRetMessage("查询工作流节点配置成功");
        return resp;
    }

    /**
     * 修改工作流节点配置
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("config-node-update")
    @LogAnnotation(value = "修改工作流节点配置", business = LogBusinessConstants.WORK_FLOW, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updateWorkflowNodeConfig(@RequestBody UpdateWorkflowConfigReq req) {
        workFlowService.updateWorkflowNodeConfig(req.getId(), req.getDetailList());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("修改工作流节点配置成功");
        return resp;
    }

    /**
     * 修改工作流配置状态
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("config-status")
    @LogAnnotation(value = "修改工作流配置状态", business = LogBusinessConstants.WORK_FLOW, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updateWorkflowConfigStatus(@RequestBody BaseDeleteReq req) {
        workFlowService.updateWorkflowConfigStatus(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("修改工作流配置状态成功");
        return resp;
    }

    /**
     * 查询流程用户组信息
     *
     * @return 流程用户组信息
     */
    @PostMapping("group-page")
    public BaseResp pageWorkflowGroupList(BaseReq req) {
        Map<String, Object> queryParam = new HashMap<>();
        queryParam.put("startNum", req.getStartNum());
        queryParam.put("pageSize", req.getPageSize());
        List<Map<String, Object>> dataList = workFlowGroupService.queryWorkflowGroupList(queryParam);
        int totalNum = workFlowGroupService.getWorkflowGroupCount(queryParam);
        BaseResp resp = new BaseResp();
        resp.setDataList(dataList);
        resp.setTotalNum(totalNum);
        resp.setRetMessage("查询流程用户组信息成功！");
        return resp;
    }

    /**
     * 新增流程用户组配置
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("group-add")
    @LogAnnotation(value = "新增流程用户组配置", business = LogBusinessConstants.WORK_FLOW, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp addWorkflowGroup(@RequestBody UpdateWorkflowGroupReq req) {
        WorkFlowGroup workFlowGroup = new WorkFlowGroup();
        workFlowGroup.setGroupName(req.getGroupName());
        workFlowGroup.setGroupSn(req.getGroupSn());
        workFlowGroup.setTreatType(req.getTreatType());
        workFlowGroup.setGroupUser(req.getGroupUser());
        workFlowGroupService.addWorkflowGroup(workFlowGroup);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("新增流程用户组配置成功");
        return resp;
    }

    /**
     * 修改流程用户组配置
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("group-update")
    @LogAnnotation(value = "修改流程用户组配置", business = LogBusinessConstants.WORK_FLOW, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updateWorkflowGroup(@RequestBody UpdateWorkflowGroupReq req) {
        WorkFlowGroup workFlowGroup = new WorkFlowGroup();
        workFlowGroup.setId(req.getId());
        workFlowGroup.setGroupName(req.getGroupName());
        workFlowGroup.setGroupSn(req.getGroupSn());
        workFlowGroup.setGroupUser(req.getGroupUser());
        workFlowGroup.setTreatType(req.getTreatType());
        workFlowGroupService.updateWorkflowGroup(workFlowGroup);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("修改流程用户组配置成功");
        return resp;
    }

    /**
     * 删除流程用户组配置信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("group-delete")
    @LogAnnotation(value = "删除流程用户组配置信息", business = LogBusinessConstants.WORK_FLOW, operateType = LogOperateConstants.OPERATE_DELETE)
    public BaseResp deleteWorkflowGroup(@RequestBody BaseDeleteReq req) {
        workFlowGroupService.deleteWorkflowGroup(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("删除流程用户组配置信息成功");
        return resp;
    }

    /**
     * 查询工作流信息
     *
     * @param req 请求信息
     * @return 工作流信息
     */
    @PostMapping("record-page")
    public BaseResp pageWorkflowRecordList(@RequestBody QueryWorkflowRecordReq req) {
        Map<String, Object> queryParam = new HashMap<>();
        queryParam.put("configId", req.getConfigId());
        queryParam.put("businessType", req.getBusinessType());
        queryParam.put("status", req.getStatus());
        queryParam.put("startNum", req.getStartNum());
        queryParam.put("pageSize", req.getPageSize());
        List<Map<String, Object>> dataList = workFlowService.queryWorkflowList(queryParam);
        int totalNum = workFlowService.getWorkflowCount(queryParam);
        BaseResp resp = new BaseResp();
        resp.setDataList(dataList);
        resp.setTotalNum(totalNum);
        resp.setRetMessage("查询工作流信息成功！");
        return resp;
    }

    /**
     * 查询工作流流转详情信息
     *
     * @param req 请求信息
     * @return 工作流信息
     */
    @PostMapping("record-detail")
    public BaseResp getWorkflowRecordDetail(@RequestBody QueryBaseReq req) {
        Map<String, Object> dataMap = workFlowService.queryWorkflowRecordDetail(req.getId());
        BaseResp resp = new BaseResp();
        resp.setDataMap(dataMap);
        resp.setRetMessage("查询工作流流转详情信息成功！");
        return resp;
    }

    /**
     * 修改工作流流转
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("record-update")
    public BaseResp updateWorkflowRecord(@RequestBody UpdateWorkflowRecordReq req) {
         workFlowService.updateWorkflowRecord(req.getFlowId(), req.getDealType(), req.getDealOpinions());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("修改工作流流转信息成功！");
        return resp;
    }

    /**
     * 生成数据
     *
     * @return 成功标识
     */
    @GetMapping("generate-data")
    public BaseResp generateWorkflowRecord() {
        workFlowService.generateWorkflowRecord();
        BaseResp resp = new BaseResp();
        resp.setRetMessage("生成数据成功！");
        return resp;
    }
}
